<script setup lang="ts">
import { Form } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, watch } from 'vue'
import { AdverData } from '@/api/lunbotu/types'
import { useI18n } from '@/hooks/web/useI18n'
import { useValidator } from '@/hooks/web/useValidator'
import { IDomEditor } from '@wangeditor/editor'
import { FormSchema } from '@/types/form'

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<AdverData>>,
    default: () => null
  }
})

const { t } = useI18n()

const schema = reactive<FormSchema[]>([
  {
    field: 'img_address',
    label: '图片地址',
    component: 'Input',
    colProps: {
      span: 24
    }
  },
  {
    field: 'img_title',
    label: '图片标题',
    component: 'Input',
    colProps: {
      span: 24
    }
  },
  //单选框是否显示
  {
    field: 'display',
    label: '是否上线',
    component: 'Select',
    componentProps: {
      options: [
        {
          label: '上线',
          value: 1
        },
        {
          label: '下线',
          value: 2
        }
      ]
    }
  },
  //轮播图类型
  {
    field: 'img_type',
    label: '图片类型',
    component: 'Select',
    componentProps: {
      options: [
        {
          label: '商品',
          value: 1
        },
        {
          label: '视频',
          value: 2
        },
        {
          label: '其他',
          value: 3
        }
      ]
    }
  },
  //开始时间
  {
    field: 'start_date',
    component: 'DatePicker',
    label: '开始时间',
    componentProps: {
      type: 'datetime',
      valueFormat: 'YYYY-MM-DD HH:mm:ss'
    }
  },
  //结束时间
  {
    field: 'expire_date',
    component: 'DatePicker',
    label: '结束时间',
    componentProps: {
      type: 'datetime',
      valueFormat: 'YYYY-MM-DD HH:mm:ss'
    }
  },
  //备注
  {
    field: 'remarks',
    label: '备注',
    component: 'Input',
    colProps: {
      span: 24 
    }
  }
])

const { register, methods, elFormRef } = useForm({
  schema
})

watch(
  () => props.currentRow,
  (currentRow) => {
    if (!currentRow) return
    const { setValues, setSchema } = methods
    setValues(currentRow)
    setSchema([
      {
        field: 'remarks',
        path: 'componentProps.defaultHtml',
        value: currentRow.remarks
      }
    ])
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  elFormRef,
  getFormData: methods.getFormData
})
</script>

<template>
  <Form :rules="rules" @register="register" />
</template>
